/** 打包脚本 打包出js文件  */
import minimist from "minimist";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import esbuild from "esbuild";

// 获取当前文件路径
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); 

// 解析参数
let args = minimist(process.argv.slice(2))

// 打包名称  包名
const target = args._[0] || 'reactivity'
// 打包方式
const format = args.f || 'iife'

const pkg = require(`../packages/${target}/package.json`)
const entry = resolve(__dirname, `../packages/${target}/src/index.ts`)
const outdir = resolve(__dirname, `../packages/${target}/dist/${target}.js`)

esbuild.context({
  entryPoints: [entry],
  outfile: outdir,
  bundle: true,
  platform: 'browser',
  sourcemap: true,
  format: format, // 打包方式 cjs | esm | iife
  globalName: pkg.buildOptions?.name, // 打包后的全局变量名

}).then((ctx) => {
  console.log('打包成功')
  return ctx.watch()
  })

